using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using SubSonic;
using SurveyHost.Core.DAL;
using System.Web;

namespace SurveyHost.Core
{
    public static class ConfigurationHelper
    {
        /// <summary>
        /// The configuration data object.  The ConfigurationHelper is responsible for loading and inserting the defaults.
        /// It is your responsibility to do the rest.  Sorry.
        /// </summary>
        public static HostConfiguration Configuration
        {
            get
            {
                return ActiveRecord<HostConfiguration>.FetchByID(HttpContext.Current.Request.Url.Host)
                    ?? GetDefaultConfiguration();
            }
        }

        /// <summary>
        /// The website name token for emails
        /// </summary>
        public const string WEBSITETOKEN = "[WEBSITEADDRESS]";
        /// <summary>
        /// The user name token
        /// </summary>
        public const string USERNAMETOKEN = "[USERNAME]";
        /// <summary>
        /// The user password token
        /// </summary>
        public const string PASSWORDTOKEN = "[PASSWORD]";
        /// <summary>
        /// The survey finish time
        /// </summary>
        public const string FINISHTIMETOKEN = "[FINISHTIME]";
        /// <summary>
        /// The administrator email token for emails
        /// </summary>
        public const string ADMINEMAILTOKEN = "[ADMINEMAIL]";
        /// <summary>
        /// A confirmation number token
        /// </summary>
        public const string CONFIRMATIONNUMBERTOKEN = "[CONFIRMATIONNUMBER]";
        /// <summary>
        /// The name given by the survey participant
        /// </summary>
        public const string GIVENNAMETOKEN = "[GIVENNAME]";
        /// <summary>
        /// The email used to send the user an invitation email
        /// </summary>
        public const string USEREMAILTOKEN = "[USEREMAIL]";

        private const string DEFAULTADMINNAME = "Survey Administrator";
        private const string DEFAULTSURVEYNAME = "SurveyHost.NET";
        private const string DEFAULTINVITATIONEMAIL =
@"Thank you for offering to participate in my study. 

You must complete the survey in one sitting.

To access the survey, please go to the following website:

	"+ WEBSITETOKEN +@"

To log in, use the following information:

    Username:   " + USERNAMETOKEN + @"
    Password:	" + PASSWORDTOKEN + @"

Proof of participation will be provided to you at the completion of the survey.

Thank you for your help!";
        private const string DEFAULTCOMPLETIONEMAIL = 
@"A survey participant has requested that you be delivered proof of completion.  Confirmation is as follows:
    
    Participant:  " + USERNAMETOKEN + @"
    Survey completed on:  " + FINISHTIMETOKEN + @"
    Confirmation number:  " + CONFIRMATIONNUMBERTOKEN + @"

If you have any questions, please contact the survey administrator at " + ADMINEMAILTOKEN + @".

*Your email address has not been recorded and will not be shared with anyone.";

        /// <summary>
        /// Loads the defaults into the database.
        /// </summary>
        private static HostConfiguration GetDefaultConfiguration()
        {
            HostConfiguration config = new HostConfiguration();
            config.HostName = HttpContext.Current.Request.Url.Host;
            config.AdministratorName = DEFAULTADMINNAME;
            config.SurveyName = DEFAULTSURVEYNAME;
            config.InvitationEmail = DEFAULTINVITATIONEMAIL;
            config.CompletionEmail = DEFAULTCOMPLETIONEMAIL;
            config.SMTPPort = 0;
            config.Save();
            return config;
        }

        /// <summary>
        /// Replaces tokens within an email with the token values from configuration
        /// </summary>
        /// <param name="tokenizedEmail">The email with tokens</param>
        /// <param name="tokenData">The token data.</param>
        /// <returns>The detokenized email</returns>
        public static string SubstituteTokens(string tokenizedEmail, TokenData tokenData)
        {
            if (string.IsNullOrEmpty(tokenizedEmail)) return tokenizedEmail;

            tokenizedEmail = tokenizedEmail.Replace(ADMINEMAILTOKEN, tokenData.AdministratorEmail);
            tokenizedEmail = tokenizedEmail.Replace(FINISHTIMETOKEN, tokenData.FinishTime);
            tokenizedEmail = tokenizedEmail.Replace(PASSWORDTOKEN, tokenData.UserPassword);
            tokenizedEmail = tokenizedEmail.Replace(USERNAMETOKEN, tokenData.UserName);
            tokenizedEmail = tokenizedEmail.Replace(WEBSITETOKEN, tokenData.WebsiteUrl);
            tokenizedEmail = tokenizedEmail.Replace(GIVENNAMETOKEN, tokenData.GivenName);
            tokenizedEmail = tokenizedEmail.Replace(CONFIRMATIONNUMBERTOKEN, tokenData.ConfirmationNumber);
            tokenizedEmail = tokenizedEmail.Replace(USEREMAILTOKEN, tokenData.UserEmail);
            return tokenizedEmail;
        }

    }

    /// <summary>
    /// Holds token substitution strings for the <seealso cref="ConfigurationHelper.SubstituteTokens"/> metho
    /// </summary>
    public class TokenData
    {
        /// <summary>
        /// The administrator's email
        /// </summary>
        public string AdministratorEmail;
        /// <summary>
        /// The time the user finished the survey
        /// </summary>
        public string FinishTime;
        /// <summary>
        /// The user's password
        /// </summary>
        public string UserPassword;
        /// <summary>
        /// The website's URL
        /// </summary>
        public string WebsiteUrl;
        /// <summary>
        /// A confirmation number
        /// </summary>
        public string ConfirmationNumber;
        /// <summary>
        /// The user's name
        /// </summary>
        public string UserName;
        /// <summary>
        /// The user's email address
        /// </summary>
        public string UserEmail;
        /// <summary>
        /// The name given by the user when submitting to the terms of the survey
        /// </summary>
        public string GivenName;
        public TokenData()
        {
          AdministratorEmail = string.Empty;
          FinishTime = string.Empty;
          UserPassword = string.Empty;
          WebsiteUrl = string.Empty;
          ConfirmationNumber = string.Empty;
          GivenName = string.Empty;
        }
    }
}
